package com.tledu.production.controller.device;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.tledu.production.core.result.AjaxResult;
import com.tledu.production.model.device.Device;
import com.tledu.production.service.device.DeviceService;
import lombok.AllArgsConstructor;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/device")
@AllArgsConstructor

public class DeviceController {

    private final DeviceService deviceService;

    //分页
    @GetMapping("/device/list")
    @ResponseBody
    public AjaxResult<PageInfo<Device>> DeviceList(Integer pageNum, Integer pageSize) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        // 下次请求会带上分页数据
        PageHelper.startPage(pageNum, pageSize);
        List<Device> list = deviceService.list();
        // 通过这个api获取pageInfo
        PageInfo<Device> page = PageInfo.of(list);
        return new AjaxResult<>(page);
    }

    //添加
    @PostMapping("/device/add")
    @ResponseBody
    public AjaxResult<Integer> DeviceAdd(Device device) {

        return new AjaxResult<>(deviceService.insert(device));
    }

    //删除
    @GetMapping("/device/delete")
    @ResponseBody
    public JSONObject delete(String deviceId) {
        int delete = deviceService.delete(deviceId);
        JSONObject jsonObject = new JSONObject();
//        {"success":delete}
//        {}对象
//        []数组
        jsonObject.put("success", delete);
        return jsonObject;
    }


    //编辑
    @PostMapping("/device/update")
    @ResponseBody
    public AjaxResult<Integer> DeviceUpdate(Device device) {

        return new AjaxResult<>(deviceService.update(device));
    }


    //按不同字段搜索
    @GetMapping("/device/search")
    @ResponseBody
    public AjaxResult<PageInfo<Device>> DeviceSearchList(Integer pageNum, Integer pageSize, String type, String searchText) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        // 下次请求会带上分页数据
        PageHelper.startPage(pageNum, pageSize);
        List<Device> list = deviceService.search(type, searchText);
        // 通过这个api获取pageInfo
        PageInfo<Device> page = PageInfo.of(list);
        return new AjaxResult<>(page);
    }

    //富文本默认值
    @RequestMapping("/device/editText/{deviceId}")
    public String edit(@PathVariable String deviceId, Model model) {
        String text = deviceService.findEdit(deviceId);
        model.addAttribute("editText",text);
        model.addAttribute("deviceId",deviceId);
        return "device/rich-text";
    }

    //富文本保存
    @RequestMapping("/device/edsave")
    @ResponseBody
    public AjaxResult<Void> edsave(String deviceId,String note){
        Device device = deviceService.findById(deviceId);
        device.setNote(note);
        deviceService.update(device);
        return new AjaxResult();
    }

    //批量删除
    @GetMapping("/device/deleteAll")
    @ResponseBody
    public AjaxResult DeviceDeleteAll(String ids){
        String[] idsarr = ids.split(",");
        int i =0;
        for(String s : idsarr){
            deviceService.delete(s);
            i++;
        }
        if (i >= 1) {
            return new AjaxResult("删除成功");
        } else {
            return new AjaxResult("删除失败", 500);
        }
    }


}
